import queue
import time
import random
import threading
import requests
import re
import os
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'}
try:
    while True:
        do_thread_num=eval(input("请输入线程数(建议8线程,最大12线程):"))#线程数
        if do_thread_num in range(1,13):
			break
		else :
			continue
except:
	print("程序异常！")

def getHTMLText(url):#定义函数
    try :
        global headers
        r = requests.get(url,timeout=30,headers=headers) #请求方式和各项参数
        r.raise_for_status() #如果状态不是200，引发HTTPError异常
        r.encoding = r.apparent_encoding #编码方式
        return r.text #返回字符串形式的网页源代码
    except : #捕捉异常
        return "产生异常！"

def ClimbWallPaper(url,j):
    global l,headers
    print("线程序号{}，获取壁纸{}".format(j,(str(url)[42:-11])))
    f = open(str(url)[42:-11],'wb')
    b = str(url)
    c = b[18:-11]
    r = requests.get(c,headers=headers)
    f.write(r.content)
    f.close()
	print("线程序号{}，壁纸{}获取完成".format(j,(str(url)[42:-11])))

def GetAllHtml():
    global y
    for i in range(1,y+1):
        url = "https://bing.ioliu.cn/?p="+str(i)
        a = getHTMLText(url)
        f = open("1.txt","a+",encoding="UTF-8")
        f.write(a)
        f.close()
        print("正在获取第{}个页面！共{}个页面！".format(i,y))

class MyThread(threading.Thread):
    def __init__(self,func,data,j):
        threading.Thread.__init__(self)
        self.data=data
        self.j=j
        self.func=func
    def run(self):
        while self.data.qsize()>0:
            self.func(self.data.get(),self.j)

if __name__=="__main__":
    y = eval(input("请输入要爬取的页面从1到多少页："))
    GetAllHtml()
#得到所有页面每一页休息0.5秒
    s=open("1.txt","rt",encoding="UTF-8").read()#打开文件
    pattern=re.compile(r"data-progressive=\"[a-zA-z]+://[^\s]*\"")#找url
    results=pattern.findall(s)#把url返回成列表
    l = len(results)#得到列表长度
    q_data=queue.Queue(l)#队列数
    for data in results:
        q_data.put(data)
    for j in range(do_thread_num):
        t1 = MyThread(ClimbWallPaper,q_data,j).start()
    os.system("del "+os.getcwd()+"\\1.txt")
#队列爬虫模型
#author=江户川_柯北
#version=0.3
#last change time=2020年3月20日17：00